<template>
	<view>
		<u-field placeholder-style="color:#757575;font-size:14px !important;"  v-model="school" required label="学校名称" placeholder="请填写" class="reset_after" :clearable="false" />
		<u-field placeholder-style="color:#757575;font-size:14px !important;"  :required="fieldStore.education.major.is_require === 1" v-if="fieldStore.education.major.is_display"
			:clearable="false" v-model="major" :label="fieldStore.education.major.field_cn" placeholder="请填写"
			class="reset_after" />
		<view class="form-line u-border-bottom" @click="showPickerEdu = !showPickerEdu">
			<view class="form-field">
				<view class="label">
					取得学历
				</view>
				<view :class="educationName ? 'text' : 'text ac'">
					{{educationName ? educationName : '请选择'}}
				</view>
			</view>
			<u-icon class="arrow-icon" name="arrow-right"></u-icon>
		</view>

		<u-picker v-model="showPickerEdu" mode="selector" :range="columnsEdu" range-key="text"
			:default-index="[educationDefaultIndex]" @confirm="onConfirmEducation" @cancel="showPickerEdu = false" />

		<view class="form-line u-border-bottom" @click="showPickerStartDate = !showPickerStartDate">
			<view class="form-field">
				<view class="label">
					入学时间
				</view>
				<view :class="starttime ? 'text' : 'text ac'">
					{{starttime ? starttime : '请选择'}}
				</view>
			</view>
			<u-icon class="arrow-icon" name="arrow-right"></u-icon>
		</view>

		<u-picker v-model="showPickerStartDate" :default-time="starttime" :params="dateParams" mode="time"
			title="选择入学时间" :start-year="minDate" :end-year="maxDate" @confirm="onConfirmStartDate"
			@cancel="showPickerStartDate = false" />
		<view class="form-line u-border-bottom" @click="showPickerEndDate = !showPickerEndDate">
			<view class="form-field">
				<view class="label">
					毕业时间
				</view>
				<view :class="endtimeName ? 'text' : 'text ac'">
					{{endtimeName ? endtimeName : '请选择'}}
				</view>
			</view>
			<u-icon class="arrow-icon" name="arrow-right"></u-icon>
		</view>
		<u-select v-model="showPickerEndDate" mode="mutil-column-auto" :list="columnsWage1" @confirm="onConfirmWage1">
		</u-select>
		<view style="margin: 32rpx 32rpx 0;">
			<u-button shape="circle" :disabled="disabled" type="primary" @click="handleSubmit">
				保存
			</u-button>
		</view>
		<view class="box_1" v-if="parseInt(id) > 0" @click="delEduModalState=true">
			删除此教育经历
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-modal v-model="delEduModalState" title="提示" content="确定删除该条教育经历吗" show-cancel-button
			@confirm="deleteCurrentEducation" @cancel="delEduModalState=false"></u-modal>
	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	export default {
		name: 'EditEducation',
		data() {
			return {
				delEduModalState: false,
				dateParams: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false,
				},
				educationDefaultIndex: 0,
				showPickerEndDate: false,
				showPickerStartDate: false,
				currentStartDate: '',
				currentEndDate: '',
				showPickerEdu: false,
				id: 0,
				school: '',
				major: '',
				education: '',
				educationName: '',
				starttime: '',
				endtime: '',
				endtimeName: '',
				todate: 0,
				minDate: '',
				maxDate: '',
				disabled:false
			}
		},
		async onLoad(e) {
			await this.$store.dispatch('getClassify', 'education')
			// 获取路由中的id
			this.id = e.id
			if (this.id > 0) {
				// 如果有id，根据id取出对应的教育经历
				let thisEducation = this.educationStore.filter(
					item => parseInt(item.id) === parseInt(this.id)
				)
				this.initData(thisEducation[0])
				// 恢复最高学历
				this.educationDefaultIndex = this.columnsEdu.findIndex(item => {
					return parseInt(item.id) === parseInt(this.education)
				})
				// 恢复入学时间和毕业时间
				// let starTimeDateArr = this.starttime.split('-')
				// this.currentStartDate = new Date(
				// 	parseInt(starTimeDateArr[0]),
				// 	parseInt(starTimeDateArr[1]) - 1
				// )
				if (!parseInt(this.todate)) {
					// 不是至今恢复结束时间
					let endTimeDateArr = this.endtime.split('-')
					this.currentEndDate = new Date(
						parseInt(endTimeDateArr[0]),
						parseInt(endTimeDateArr[1]) - 1
					)
				}
			}
		},
		mounted() {
			this.initDate()
		},
		computed: {
			columnsEdu() {
				return this.$store.state.classifyEdu
			},
			educationStore() {
				return this.$store.state.resume.education_list
			},
			fieldStore() {
				return this.$store.state.resume.field_rule
			},
			columnsWage1() {
			  return this.date()
			},
		},
		methods: {
			onConfirmWage1(values) {
				// 修改
				if(values[0].label == '至今'){
					this.endtime = ''
					this.endtimeName = '至今'
					this.todate = 1
				} else {
					var y = parseInt(values[0].label)
					var m = parseInt(values[1].label) >= 10 ? parseInt(values[1].label) : '0' + parseInt(values[1].label)
					this.endtime = y + '-' + m
					this.endtimeName = y + '-' + m
					this.todate = 0
				}
			},
			// 修改
			// 基础数据
			date() {
			  const nowDate = new Date()
			  const nowYear = nowDate.getFullYear()
			  const minYear = nowYear - 59
			  const yearArr = ['至今']
			  const monthArr = []
			  const monthArr1 = []
			  const arr = []
			  for (let i = 0; i < nowYear - minYear; i++) {
				yearArr.push((nowYear - i) + '年')
			  }
			  for (let j = 0; j < 12; j++) {
				  var m = (j + 1) >= 10 ? (j + 1) : '0' + (j + 1)
				monthArr.push({
				  value: m + '月' ,
				  label: m + '月'
				})
				monthArr1.push('')
			  }
			  for (let k = 0; k < yearArr.length; k++) {
				if (yearArr[k] == '至今') {
				  arr.push({
					label: yearArr[k],
					value: yearArr[k],
					children:monthArr1
				  })
				} else {
				  arr.push({
					label: yearArr[k],
					value: yearArr[k],
					children: monthArr
				  })
				}
			  }
			  return arr
			},
			// 初始化接口返回数据
			initData(interfaceEducation) {
				this.school = interfaceEducation.school
				this.major = interfaceEducation.major
				this.education = interfaceEducation.education
				this.educationName = this.columnsEdu.filter(
					item => parseInt(item.id) === parseInt(this.education)
				)[0].text
				this.starttime = this.dateFormat(
					new Date(interfaceEducation.starttime * 1000)
				)
				if (parseInt(interfaceEducation.todate)) {
					// 至今
					this.endtime = ''
					this.endtimeName = '至今'
					this.todate = 1
				} else {
					this.endtime = this.dateFormat(
						new Date(interfaceEducation.endtime * 1000)
					)
					this.endtimeName = this.dateFormat(
						new Date(interfaceEducation.endtime * 1000)
					)
					this.todate = 0
				}
			},
			// 设置日期插件默认值
			initDate() {
				let nowDate = new Date()
				let nowYear = nowDate.getFullYear()
				let minYear = nowYear - 59

				this.minDate = minYear
				this.maxDate = nowYear
				this.currentStartDate = nowDate
				this.currentEndDate = nowDate
			},
			// 选择取得学历
			onConfirmEducation(index) {
				this.education = this.columnsEdu[index].id
				this.educationName = this.columnsEdu[index].text
				this.showPickerEdu = !this.showPickerEdu
			},
			// 选择入学时间
			onConfirmStartDate(value) {
				this.starttime = value.year + '-' + value.month
				this.showPickerStartDate = !this.showPickerStartDate
			},
			// 保存
			handleSubmit() {
				if (!this.school) {
					this.$refs.uTips.show({
						title: '请填写学校名称',
						type: 'error'
					})
					return false
				}
				if (this.fieldStore.education.major.is_require) {
					if (!this.major) {
						this.$refs.uTips.show({
							title: `请填写${this.fieldStore.education.major.field_cn}`,
							type: 'error'
						})
						return false
					}
				}
				if (!this.education) {
					this.$refs.uTips.show({
						title: '请选择学历',
						type: 'error'
					})
					return false
				}
				if (!this.starttime) {
					this.$refs.uTips.show({
						title: '请选择入学时间',
						type: 'error'
					})
					return false
				}
				if (parseInt(this.todate) === 0) {
					if (!this.endtime) {
						this.$refs.uTips.show({
							title: '请选择毕业时间',
							type: 'error'
						})
						return false
					}
				}
				this.disabled = true
				http
					.post(api.resume_education_save, {
						id: this.id,
						school: this.school,
						major: this.major,
						education: this.education,
						starttime: this.starttime,
						endtime: this.endtime,
						todate: this.todate
					})
					.then(res => {
						if (parseInt(res.data.code) === 200) {
							this.$refs.uTips.show({
								title: res.data.message,
								type: 'success'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							}, 1500)
						} else {
							this.$refs.uTips.show({
								title: res.data.message,
								type: 'error'
							})
						}
						this.disabled = false
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 删除此条教育经历
			deleteCurrentEducation() {
				http
					.post(api.resume_education_delete, {
						id: this.id
					})
					.then(res => {
						if (parseInt(res.data.code) === 200) {
							this.$refs.uTips.show({
								title: res.data.message,
								type: 'success'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							}, 500)
						} else {
							this.$refs.uTips.show({
								title: res.data.message,
								type: 'error'
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 格式化日期
			dateFormat(time) {
				let year = time.getFullYear()
				let month = time.getMonth() + 1
				return `${year}-${month}`
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box_1 {
		font-size: 26rpx;
		color: #999999;
		text-align: center;
		width: 100%;
		padding: 30rpx 0;
	}
</style>
